package cn.tp.ksoft.civil_driver.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * SystemPreference 系统特征库
 * @author janlin  2016-3-25
 *
 */
public class SystemPreference {

	private static final String SYSTEM_VALUES_NAME = "system_value";
	// 系统参数
	public static final String SYSTEM_VALUE_NAME_SERVER_IP = "system_value_server_ip";
	
	public static final String SYSTEM_VALUE_USER_TOKEN = "system_value_user_token";
	
	/**已登录**/
	public static final String SYSTEM_VALUE_USER_HASlOGINED = "system_value_user_haslogined";

	public static final String USER_LOGIN_INFO = "user_login_info";
	
	public static final String SYSTEM_USER_LOGIN_EMAILNAME = "system_value_user_email";
	public static final String SYSTEM_USER_LOGIN_PHONENAME = "system_value_user_phone";
	public static final String SYSTEM_USER_LOGIN_NICKNAME = "system_value_user_nickname";

	/**
	 * 写入系统性参数
	 * @param ctx
	 * @param key
	 * @param value
	 */
	public static void writeSystemValue(Context ctx, String key, String value) {
		SharedPreferences system_value = ctx.getSharedPreferences(
				SYSTEM_VALUES_NAME, 0);
		Editor eidt = system_value.edit();
		eidt.putString(key, value);
		eidt.commit();
	}

	/**
	 * 读出系统性参数
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static String readSystemValue(Context ctx, String key) {
		return readSystemValue(ctx, key, "");
	}

	public static String readSystemValue(Context ctx, String key,
			String defaultvalue) {
		SharedPreferences system_value = ctx.getSharedPreferences(
				SYSTEM_VALUES_NAME, 0);
		return system_value.getString(key, defaultvalue);
	}
	
	/**
	 * 写入系统性参数-int
	 * @param ctx
	 * @param key
	 * @param value
	 */
	public static void writeSystemIntValue(Context ctx, String key, int value){
		SharedPreferences system_value = ctx.getSharedPreferences(SYSTEM_VALUES_NAME, 0);
		Editor eidt = system_value.edit();
		eidt.putInt(key, value);
		eidt.commit();
	}
	
	/**
	 * 读出系统性参数-int
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static int readSystemIntValue(Context ctx, String key){
		return readSystemIntValue(ctx, key, 0);
	}
	public static int readSystemIntValue(Context ctx, String key, int defaultvalue){
		SharedPreferences system_value = ctx.getSharedPreferences(SYSTEM_VALUES_NAME, 0);
		return system_value.getInt(key, defaultvalue);
	}
	/**
	 * 写入系统性参数-bool
	 * @param ctx
	 * @param key
	 * @param value
	 */
	public static void writeSystemBooleanValue(Context ctx, String key, boolean value){
		SharedPreferences system_value = ctx.getSharedPreferences(SYSTEM_VALUES_NAME, 0);
		Editor eidt = system_value.edit();
		eidt.putBoolean(key, value);
		eidt.commit();
	}
	/**
	 * 读出系统性参数-bool
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static boolean readSystemBooleanValue(Context ctx, String key){
		return readSystemBooleanValue(ctx, key, false);
	}
	public static boolean readSystemBooleanValue(Context ctx, String key, Boolean defaultvalue){
		SharedPreferences system_value = ctx.getSharedPreferences(SYSTEM_VALUES_NAME, 0);
		return system_value.getBoolean(key, defaultvalue);
	}
	
	/**
	 * 写入用户登录信息
	 * @param ctx
	 * @param key
	 * @param value
	 */
	public static void writeUserValue(Activity activity, String key, String value) {
		SharedPreferences sp =activity.getSharedPreferences(
				USER_LOGIN_INFO,Activity.MODE_PRIVATE);
		Editor eidt = sp.edit();
		eidt.putString(key, value);
		eidt.commit();	
	}

	/**
	 * 读出用户登录参数
	 * @param ctx
	 * @param key
	 * @return
	 */
	public static String readUserValue(Activity ctx, String key) {
		return readUserValue(ctx, key, "");
	}

	public static String readUserValue(Activity ctx, String key,
			String defaultvalue) {
		SharedPreferences sp = ctx.getSharedPreferences(
				USER_LOGIN_INFO,Activity.MODE_PRIVATE);
		return sp.getString(key,defaultvalue);
	}
}
